<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ArticleController extends Controller
{

    public function articleInfo($id)
    {
        $data = array();
        $data['article_info'] = $this->_articleService->getArticle($id);
        /*前一篇文章*/
        $pre_id = $id - 1;
        $pre_article_info = $this->_articleService->getArticle($pre_id);
        if (!isset($pre_article_info)) {
            $pre_id--;
            $num = 0;
            while (!isset($pre_article_info) && $pre_id > 1 && $num < 20) {
                $pre_article_info = $this->_articleService->getArticle($pre_id);
                $num++;
            }
        }
        /*后一篇文章*/
        $next_id = $id + 1;
        $next_article_info = $this->_articleService->getArticle($next_id);
        if (!isset($next_article_info)) {
            $next_id++;
            $num = 0;
            while (!isset($pre_article_info) && $next_article_info->isEmpty() && $num < 20) {
                $next_article_info = $this->_articleService->getArticle($next_id);
                $num++;
            }
        }
        $data['pre_article_info'] = $pre_article_info;
        $data['next_article_info'] = $next_article_info;
        return $this->layout_view('article/info', $data);
    }

    /**
     * showdoc
     * @catalog 文章
     * @title 点赞
     * @description 点赞接口
     * @method post
     * @url  https://blog.yezt88.com/api/v1/article/like
     * @param id 必选 string 文章ID
     * @return {"error":0,"data":{}}
     * @return_param id int 更新Id
     * @remark 这里是备注信息
     * @number 1
     */
    public function like()
    {
        $id = Request()->post('id');
        $result = $this->_articleService->updateArticleLike($id);
        return $this->show_result(0, $result);
    }
}
